home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / font3d10.zip / List.H < prev    next >
C/C++ Source or Header  |  1994-09-15  |  5KB  |  149 lines

  1. //===========================================================================================================
  2. //  List.H
  3. //
  4. //  Copyright (c) 1994 by Todd A. Prater
  5. //  All rights reserved.
  6. //
  7. //-----------------------------------------------------------------------------------------------------------
  8. //
  9. //  A simple linked-list class.  All operations are defined as 'inline', but 'Empty' and 'Add' might not
  10. //  be expanded, depending on the compiler.
  11. //
  12. //  Operations:  LIST(void)..............................Creates an empty list.
  13. //               Empty(void).............................Empties a list by deleting all its objects.
  14. //               Add(T*).................................Adds an object to the list.  The new object should
  15. //                                                       already be created before the call to 'Add' is
  16. //                                                       made.
  17. //               Count(void).............................Returns the number of objects in the list.
  18. //               First(void).............................Returns a pointer to the first object in the list.
  19. //               Current(void)...........................Returns a pointer to the current object in the
  20. //                                                       list.
  21. //               Last(void)..............................Returns a pointer to the last object in the list.
  22. //               Next(void)..............................Returns a pointer to the next object in the list.
  23. //               gotoFirst...............................Moves the window over the first object in the list.
  24. //               gotoNext................................Moves the window over the next object in the list.
  25. //               gotoLast................................Moves the window over the last object in the list.
  26. //
  27. //
  28. //===========================================================================================================
  29.  
  30. #ifndef __List_H__
  31. #define __List_H__
  32.  
  33.   #include <stdlib.h>
  34.   #include <iostream.h>
  35.   #include <stddef.h>
  36.  
  37.   #include "Config.H"
  38.  
  39.  
  40.   template <class T>
  41.   class LISTLINK
  42.   {
  43.     private: T*           obj;
  44.              LISTLINK<T>* next;
  45.  
  46.     public:  LISTLINK (void)
  47.              {
  48.                obj = NULL;
  49.                next = NULL;
  50.              }
  51.  
  52.              LISTLINK (T* object, LISTLINK<T>* tonext)
  53.              {
  54.                obj  = object;
  55.                next = tonext;
  56.              }
  57.  
  58.              T*           Obj     (void)            { return obj; };
  59.              LISTLINK<T>* Next    (void)            { return next; };
  60.              void         setObj  (T* object)       { obj = object; };
  61.              void         setNext (LISTLINK<T>* n)  { next = n; };
  62.  
  63.   };
  64.  
  65.  
  66.   template <class T>
  67.   class LIST
  68.   {
  69.     private: INT          count;
  70.              LISTLINK<T>* first;
  71.              LISTLINK<T>* current;
  72.              LISTLINK<T>* last;
  73.  
  74.     public:  LIST(void)
  75.              {
  76.                count = 0;
  77.                first = NULL;
  78.                current = NULL;
  79.                last = NULL;
  80.              }
  81.  
  82.              void Empty()
  83.              {
  84.                INT i;
  85.                LISTLINK<T>* tempnext;
  86.                gotoFirst();
  87.                for (i=0;i<count;i++)
  88.                {
  89.                   delete current->Obj();
  90.                   gotoNext();
  91.                }
  92.                gotoFirst();
  93.                for (i=0;i<count;i++)
  94.                {
  95.                   tempnext = current->Next();
  96.                   if (tempnext!=NULL) delete current;
  97.                   current = tempnext;
  98.                }
  99.                count=0;
  100.                first=NULL;
  101.                current=NULL;
  102.                last=NULL;
  103.              }
  104.  
  105.  
  106.              BYTE Add (T* object)
  107.              {
  108.                LISTLINK<T>* newlink;
  109.  
  110.                newlink = new LISTLINK<T>(object,NULL);
  111.  
  112.                if (!newlink)
  113.                {
  114.                  cout << "ERROR: Out of Memory in LIST";
  115.                  abort();
  116.                }
  117.  
  118.                if (count==0)
  119.                {
  120.                  first = newlink;
  121.                  current = newlink;
  122.                  last = newlink;
  123.                  last->setNext(NULL);
  124.                }
  125.                else
  126.                {
  127.                  last->setNext(newlink);
  128.                  last = newlink;
  129.                }
  130.  
  131.                count++;
  132.                return (TRUE);
  133.  
  134.              };
  135.  
  136.              ULONG Count     (void)  { return count;                  };
  137.              T*    First     (void)  { return first->Obj();           };
  138.              T*    Current   (void)  { return current->Obj();         };
  139.              T*    Last      (void)  { return last->Obj();            };
  140.              T*    Next      (void)  { return current->Next()->Obj(); };
  141.              void  gotoFirst (void)  { current = first;               };
  142.              void  gotoLast  (void)  { current = last;                };
  143.              void  gotoNext  (void)  { if (current==last) return;
  144.                                       current = current->Next();     };
  145.  
  146.   };
  147.  
  148. #endif
  149.